package development;

import java.util.Hashtable;

import state.GameForm;
import state.GameGlobal;
import state.Key;
import state.component.Checkbox;
import state.util.Color;

public class FrmTestCheckbox extends GameForm {
  private Checkbox dieuKhoanCheckbox;
  private Checkbox diChoiCheckbox;
  private Checkbox thichDiPhuotCheckbox;
  
  private int chooseIndex;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmTestCheckbox();
    }
    return singleton;
  }
  
  public void init(Hashtable parameters) {
    manager.setBackGroundColor(Color.BLACK_CODE);
    chooseIndex = 0;
    
    dieuKhoanCheckbox = new Checkbox(manager, 30, 50).setLabel("Bạn đồng ý với điều khoản: ").setDrawLabelFirst(true);
    dieuKhoanCheckbox.setStyle(Global.CHECK_BOX_STYLE).setFocused(true);
    
    diChoiCheckbox = new Checkbox(manager, 30, 80).setLabel("Cuối tuần đi chơi nhé? ").setDrawLabelFirst(true);
    diChoiCheckbox.setStyle(Global.CHECK_BOX_STYLE);
    
    thichDiPhuotCheckbox = new Checkbox(manager, 30, 110).setLabel("Bạn có thích đi phượt không? ").setDrawLabelFirst(true);
    thichDiPhuotCheckbox.setStyle(Global.CHECK_BOX_STYLE);
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.DOWN:
      chooseIndex = (chooseIndex + 1) % 3;
      updateFocus();
      break;
    case Key.UP:
      chooseIndex = (--chooseIndex < 0) ? 2 : chooseIndex;
      updateFocus();
      break;
    case Key.FIRE:
      pressKey();
      break;
    case Key.SOFT_RIGHT:
      GameGlobal.nextState(FrmMenu.instance(), null);
      break;
    }
  }
  
  private void updateFocus() {
    dieuKhoanCheckbox.setFocused(false);
    diChoiCheckbox.setFocused(false);
    thichDiPhuotCheckbox.setFocused(false);
    switch (chooseIndex) {
    case 0:
      dieuKhoanCheckbox.setFocused(true);
      break;
    case 1:
      diChoiCheckbox.setFocused(true);
      break;
    case 2:
      thichDiPhuotCheckbox.setFocused(true);
      break;
    }
  }
  
  private void pressKey() {
    switch (chooseIndex) {
    case 0:
      dieuKhoanCheckbox.setChecked(!dieuKhoanCheckbox.isChecked());
      break;
    case 1:
      diChoiCheckbox.setChecked(!diChoiCheckbox.isChecked());
      break;
    case 2:
      thichDiPhuotCheckbox.setChecked(!thichDiPhuotCheckbox.isChecked());
      break;
    }
  }
  
  protected void destroy() {
    if (dieuKhoanCheckbox != null) {
      dieuKhoanCheckbox.detroy();
      dieuKhoanCheckbox = null;
    }
  }
}
